$(function() {
	var date = new Date();
	var year = dateFormat("YYYY", date);
	staticMaps(year,"");
})

/**
 * 按年、月查询
 */
function selectByYearOrMonth(){
	var years = $("#years").val();
	var months = $("#months").val();
	staticMaps(years,months);
}
/**
 * 清空查询条件
 */
function reloadYearOrMonth(){
	// $("#yearandmonth").html("");
	// var date = new Date();
	// var year = dateFormat("YYYY", date);
	// newStaticMaps(year,"");
	location.reload();
}
/**
 * 查询费用排行，并存入全局对象
 */
function staticMaps(year,month){
	$.ajax({
		url: 'http://localhost:9000/statistical/freightrank/moneyfreight?year='+year+'&month='+month,
		type: 'get',
		dataType: 'json',
		success: function(map) {
			staticMapsPo=map;  //此为全局变量

			var yearandmonth ="";
			if(year!=null && year!=""){
				yearandmonth+=year+"年";
			}
			if(month!=null && month!=""){
				yearandmonth+=month+"月";
			}
			$("#yearandmonth").html(yearandmonth);

			moneyFreights = staticMapsPo.moneyFreight;
			
			names = new Array(); //姓名
			reduceFunds = new Array();    //减款
			payKickbacks = new Array();    //付回扣
			moneyOfChangePays = new Array();    //代收货款变更后金额
			carryGoodsFees = new Array();    //送货费
			carriages = new Array();    //运费
			insurances = new Array();    //保险费
			
			var x =moneyFreights.length;
			if(x>6){
				x=5;
			}
			for (var i = 0; i < x; i++) {
				var totalFreight = moneyFreights[i];
				names.push(totalFreight.name);					//姓名
				reduceFunds.push(totalFreight.reduceFund);    //减款
				payKickbacks.push(totalFreight.payKickback);   //付回扣
				moneyOfChangePays.push(totalFreight.moneyOfChangePay);    //代收货款变更后金额
				carryGoodsFees.push(totalFreight.carryGoodsFee);    //送货费
				carriages.push(totalFreight.carriage);    //运费
				insurances.push(totalFreight.insurance);  //保险费
			}
			chartOne();
		}
	})
}
/**
 * @param {Object} map
 * 图表结构
 */
function chartOne() {
	// 基于准备好的dom，初始化echarts实例
	var myChart = echarts.init(document.getElementById('main'));
	// app.title = '堆叠柱状图';
	option = {
	    tooltip : {
	        trigger: 'axis',
	        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
	            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
	        }
	    },
	    legend: {
	        data:['减款','代收货款变更后金额','预付金额','送货费','运费','保险费']
	    },
	    grid: {
	        left: '3%',
	        right: '4%',
	        bottom: '3%',
	        containLabel: true
	    },
	    xAxis : [
	        {
	            type : 'category',
				data : names
	            // data : ['小小','中中','大大','蜘蛛侠','蝙蝠侠']
	        }
	    ],
	    yAxis : [
	        {
	            type : 'value'
	        }
	    ],
	    series : [
	        {
	            name:'减款',
	            type:'bar',
				data : reduceFunds
	            // data:[-320, -332, -301, -334, -390]
	        },
	        {
	            name:'代收货款变更后金额',
	            type:'bar',
	            // stack: '广告',
	            data:payKickbacks
	        },
	        {
	            name:'预付金额',
	            type:'bar',
	            // stack: '广告',
	            data:moneyOfChangePays
	        },
	        {
	            name:'送货费',
	            type:'bar',
	            // stack: '广告',
	            data:carryGoodsFees
	        },
	        {
	            name:'运费',
	            type:'bar',
	            data:carriages
	        },
	        {
	            name:'保险费',
	            type:'bar',
	            barWidth : 5,
	            // stack: '搜索引擎',
	            data:insurances
	        }
	    ]
	};


	// 使用刚指定的配置项和数据显示图表。
	myChart.setOption(option);
}

/**
 * 图表2，多条显示 test成功后复制过来
 */
function chartTwo() {
	// 基于准备好的dom，初始化echarts实例
	var myChart = echarts.init(document.getElementById('main'));
	
	option = {
		title: {
			text: '运费排行'
			// subtext: '纯属虚构'
		},
		tooltip: {
			trigger: 'axis'
		},
		legend: {
			data: ['总运费']
		},
		toolbox: {
			show: true,
			feature: {
				dataView: {
					show: false,
					readOnly: false
				},
				magicType: {
					show: true,
					type: ['line', 'bar']
				},
				restore: {
					show: true
				},
				saveAsImage: {
					show: true
				}
			}
		},
		calculable: true,
		xAxis: [{
			type: 'category',
			data: names
		}],
		yAxis: [{
			type: 'value'
		}],
		series: [
		{
			name: '总运费',
			type: 'bar',
			data: carriages,
			markPoint: {
				data: [{
						type: 'max',
						name: '最大值'
					},
					{
						type: 'min',
						name: '最小值'
					}
				]
			},
			markLine: {
				data: [{
					type: 'average',
					name: '平均值'
				}]
			}
		},
		{
			name: '总送货费',
			type: 'bar',
			data: carryGoodsFees,
			markPoint: {
				data: [{
						type: 'max',
						name: '最大值'
					},
					{
						type: 'min',
						name: '最小值'
					}
				]
			},
			markLine: {
				data: [{
					type: 'average',
					name: '平均值'
				}]
			}
		}
		]
	};
	// 使用刚指定的配置项和数据显示图表。
	myChart.setOption(option);
}

function dateFormat(fmt, date) {
	var ret;
	var opt = {
		"Y+": date.getFullYear().toString(),        // 年
		"m+": (date.getMonth() + 1).toString(),     // 月
		"d+": date.getDate().toString(),            // 日
		"H+": date.getHours().toString(),           // 时
		"M+": date.getMinutes().toString(),         // 分
		"S+": date.getSeconds().toString()          // 秒
		// 有其他格式化字符需求可以继续添加，必须转化成字符串
	};
	for (var k in opt) {
		ret = new RegExp("(" + k + ")").exec(fmt);
		if (ret) {
			fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, "0")))
		};
	};
	return fmt;
}